#include "D20CampaignModel.h"
#include <ItemRoot.h>
#include <ItemReference.h>
#include <QDebug>


D20CampaignModel::D20CampaignModel(QObject *parent) :
    D20BaseModel(parent)
{
    addGroup(DiceGroup,         QT_TR_NOOP("Dice"));
    addGroup(AttributeGroup,    QT_TR_NOOP("Attribute"));
    addGroup(SkillGroup,        QT_TR_NOOP("Skill"));
    addGroup(FeatGroup,         QT_TR_NOOP("Feat"));
    addGroup(SpecialGroup,      QT_TR_NOOP("Special Ability"));

    addGroup(HitDiceGroup,      QT_TR_NOOP("Hit Dice"));
    addGroup(SkillPointsGroup,  QT_TR_NOOP("Skill Points"));

    addGroup(BaseBonusGroup,    QT_TR_NOOP("Base Bonus"));
    addGroup(SaveBonusGroup,    QT_TR_NOOP("Save Bonus"));
}




QStringList D20CampaignModel::skills(const bool &sub_skills) const
{
    QStringList ret;
    foreach (QModelIndex index, itemIndexes(SkillGroup))
    {
        AbstractItem* item = indexItem(index);
        if (!item) continue;

        QString name = item->name();
        if (name.isEmpty()) continue;

        if (!sub_skills)
        {
            ret << name;
            continue;
        }

        QStringList subSkillNames = subSkills(name);
        if (subSkillNames.isEmpty())
        {
            ret << name;
            continue;
        }

        foreach (QString subSkillName, subSkillNames)
        {
            if (subSkillName.isEmpty()) continue;
            ret << QString("%1 (%2)").arg(name).arg(subSkillName);
        }

        //qDebug() << Q_FUNC_INFO << "Added sub skills for:" << name;
    }

    return ret;
}

QStringList D20CampaignModel::feats() const
{
    QStringList ret;
    foreach (QModelIndex index, itemIndexes(FeatGroup))
    {
        AbstractItem* item = indexItem(index);
        if (!item) continue;

        QString name = item->data(AbstractItem::NameData).toString();
        if (name.isEmpty()) name = tr("Unknown Feat");
        ret << name;
    }

    return ret;
}


QStringList D20CampaignModel::specials() const
{
    QStringList ret;
    foreach (QModelIndex index, itemIndexes(SpecialGroup))
    {
        AbstractItem* item = indexItem(index);
        if (!item) continue;

        QString name = item->data(AbstractItem::NameData).toString();
        if (name.isEmpty()) name = tr("Unknown Special Ability");
        ret << name;
    }

    return ret;
}



QStringList D20CampaignModel::baseBonuses() const
{
    QStringList ret;

    foreach (QModelIndex index, itemIndexes(BaseBonusGroup))
    {
        AbstractItem* item = indexItem(index);
        if (!item) continue;

        QString name = item->data(AbstractItem::NameData).toString();
        if (name.isEmpty()) name = tr("Unknown Base Bonus");
        ret << name;
    }

    return ret;
}



QStringList D20CampaignModel::saveBonuses() const
{
    QStringList ret;

    foreach (QModelIndex index, itemIndexes(SaveBonusGroup))
    {
        AbstractItem* item = indexItem(index);
        if (!item) continue;

        QString name = item->name();
        if (name.isEmpty()) name = tr("Unknown Save Bonus");
        ret << name;
    }

    return ret;
}


QStringList D20CampaignModel::subSkills(const QString& skill) const
{
    QStringList ret;

    QModelIndex searchIndex = itemIndex(SkillGroup, skill).child(0, AbstractItem::NameData);
    if (!searchIndex.isValid()) return ret;

    QModelIndexList foundIndexes = match(searchIndex,
                                         Qt::EditRole,
                                         "*",
                                         -1,
                                         Qt::MatchWildcard);

    foreach (QModelIndex subSkillIndex, foundIndexes)
    {
        AbstractItem* item = indexItem(subSkillIndex);
        if (!item || IS_ROOT(item) || IS_REFERENCE(item)) continue;

        QString name = item->name();
        if (name.isEmpty()) name = tr("Unknown Sub Skill");

        ret << name;
    }
    return ret;
}


QVariantMap D20CampaignModel::options(const int &groupId, const QString &name) const
{
    QVariantMap ret;

//    qDebug() << Q_FUNC_INFO << "A";
    QModelIndex optionParent;
    if (!name.isEmpty()) optionParent = itemIndex(groupId, name);
    else                 optionParent = groupIndex(groupId);

    if (!optionParent.isValid()) return ret;

//    qDebug() << Q_FUNC_INFO << "B";
    QModelIndex option = optionParent.child(0, AbstractItem::IdData);
    while (option.isValid())
    {
        AbstractItem* optionItem = indexItem(option);
        if (!optionItem) continue;

        QString name = optionItem->name();

        QModelIndex optionReference = option.child(0, Item::ValueData);
        if (!optionReference.isValid())
        {
            ret.insert(name, name);
        }
        else
        {
            ret.insert(name, optionReference.data());
        }

        option = option.sibling(option.row()+1, AbstractItem::IdData);
    }

    return ret;
}




